import RootLayout from "@/pages/components/Layout";
import loginImg from '../../../public/login.png'
import Image from "next/image";
import styles from './index.module.css'
import {useRouter} from "next/router";

export default function Login() {
    const router = useRouter();

    function getDomain() {
        const protocol = window.location.protocol; // 获取协议，例如 'http:'
        const hostname = window.location.hostname; // 获取主机名，例如 'www.example.com'
        const port = window.location.port ? `:${window.location.port}` : ''; // 获取端口号，如果有的话
        return `${protocol}//${hostname}${port}`
    }
    function jumpToWechatForAuth() {
        const query = router.query
        console.log('query from login page', query)

        let redirect_uri = getDomain() + '/login_redirect'
        // if (query.returnUrl) {
            redirect_uri += '?returnUrl=' + encodeURIComponent(query.returnUrl as string)
       // }
        console.log('redirect_uri', redirect_uri)
        const url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx83e6d759efb54349&redirect_uri=' + encodeURIComponent(redirect_uri) + '&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect'
        console.log('url', url)
         //window.location.href = url
    }

    return (
        <RootLayout>
            <div className={styles.loginCover}>
                <Image width={200} src={loginImg} alt="logo" className="w-20 h-20"/>
            </div>
            <button className={styles.loginBtn}
                    onClick={() => jumpToWechatForAuth()}>点击登录
            </button>
        </RootLayout>
    )
}
